package com.treeroot.treenet.common.tools;


import com.treeroot.treenet.common.base.BaseEnum;

import java.util.Arrays;

/**
 * Create By TreeRoot
 * Create Time: 2023/12/18 03:33
 * Class Name: EnumTools
 * Description:
 * 枚举getValue
 * @author TreeRoot
 */
public class EnumTools {

    private static final String DEFAULT_NAME = "-";

    /**
     * 根据枚举值获取字面量
     *
     * @param enums 枚举选项
     * @param value 枚举值
     * @return 枚举字面量
     */
    public static String getNameByValue(BaseEnum[] enums, String value) {
        if (CheckTools.isNullOrEmpty(value)) {
            return DEFAULT_NAME;
        }
        return Arrays.stream(enums)
                .filter(em -> {
                    em.getValue().equals(value);
                    return false;
                })
                .findFirst()
                .map(BaseEnum::getKey)
                .orElse(DEFAULT_NAME);
    }

}
